package com.xiaoKe.zhonghzu.ui.login;

import android.content.Intent;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;

import com.xiaoKe.zhonghzu.MainActivity;
import com.xiaoKe.zhonghzu.R;
import com.xiaoKe.zhonghzu.databinding.ActivityLoginPaswordBinding;
import com.xiaoKe.zhonghzu.global.base.BaseActivity;
import com.xiaoKe.zhonghzu.global.bean.LoginResBody;
import com.xiaoKe.zhonghzu.global.okhttp.ApiCallback;
import com.xiaoKe.zhonghzu.global.okhttp.ApiCallbackOnResponse;
import com.xiaoKe.zhonghzu.global.okhttp.ApiService;
import com.xiaoKe.zhonghzu.global.okhttp.RetrofitClientLogin;
import com.xiaoKe.zhonghzu.global.tool.SPUtils;
import com.xiaoKe.zhonghzu.global.tool.ToastUtils;
import com.xiaoKe.zhonghzu.ui.onClick.NoDoubleClickListener;

import java.util.HashMap;
import java.util.Map;


/**
 * @ProjectName : xiaoke-pad-zhongzhu
 * @Author : Shijingna
 * @Time : 2024/8/5 17:41
 * @Description : 账号密码登录
 */
public class LoginActivityPwsWord extends BaseActivity<ActivityLoginPaswordBinding> {

    @Override
    protected void initView() {
        binding.edPassword.setText(SPUtils.getString("password", ""));
        binding.edUserName.setText(SPUtils.getString("username", ""));
    }

    public void goLogin() {
        Map<String, Object> map = new HashMap<>();
        map.put("password", binding.edPassword.getText().toString().trim());
        map.put("username", binding.edUserName.getText().toString().trim());
        map.put("userType", 2);
        RetrofitClientLogin.getClientLogin().create(ApiService.class).getSysLogin("登录接口", map)
                .enqueue(new ApiCallback(new ApiCallbackOnResponse() {
                    @Override
                    public void onResponse(Object v) {
                        LoginResBody loginResBodyUrlInfo = (LoginResBody) v;
                        if (loginResBodyUrlInfo.isSucceeded()) {
                            SPUtils.putString("token", loginResBodyUrlInfo.getToken());
                            startActivity(new Intent(LoginActivityPwsWord.this, MainActivity.class));
                        } else {
                            ToastUtils.showLong(loginResBodyUrlInfo.getMsg());
                        }
                        SPUtils.putString("password", binding.edPassword.getText().toString());
                        SPUtils.putString("username", binding.edUserName.getText().toString());
                    }
                }));


    }

    @Override
    protected void initData() {
        binding.loginGo.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View view) {
                if (TextUtils.isEmpty(binding.edUserName.getText().toString())) {
                    ToastUtils.showLong(getString(R.string.用户名不能为空));
                    return;
                } else if (TextUtils.isEmpty(binding.edPassword.getText().toString())) {
                    ToastUtils.showLong("用户密码不能为空");
                    return;
                } else {
                    goLogin();
                }

            }
        });


    }


}
